package com.shyfu.entities;

import org.hibernate.validator.constraints.NotEmpty;

import javax.persistence.*;
import javax.validation.constraints.Size;
import java.sql.Timestamp;
import java.util.List;

/**
 * Created by Svitlana on 03-Apr-14.
 */
@Entity
@Table(name = "AUTHORS", schema = "SHYFU", catalog = "")
public class Author {
    private long authorId;
    @NotEmpty
    @Size(max = 100)
    private String firstName;
    @NotEmpty
    @Size(max = 100)
    private String lastName;
    @NotEmpty
    private Timestamp birthDate;
    private Timestamp deathDate;
    @Size(max = 100)
    private String img;
    @Size(max = 4000)
    private String bio;
    private List<Book> books;

    @Id
    @Column(name = "AUTHOR_ID", nullable = false, insertable = true, updatable = true, precision = 0)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "author_seq")
    @SequenceGenerator(name = "author_seq", sequenceName = "AUTHORIDSEQUENCE", allocationSize = 1)
    public long getAuthorId() {
        return authorId;
    }

    public void setAuthorId(long authorId) {
        this.authorId = authorId;
    }

    @Basic
    @Column(name = "FNAME", nullable = false, insertable = true, updatable = true, length = 100)
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String fname) {
        this.firstName = fname;
    }

    @Basic
    @Column(name = "LNAME", nullable = false, insertable = true, updatable = true, length = 100)
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lname) {
        this.lastName = lname;
    }

    @Basic
    @Column(name = "BDATE", nullable = false, insertable = true, updatable = true)
    public Timestamp getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(Timestamp bdate) {
        this.birthDate = bdate;
    }

    @Basic
    @Column(name = "DDATE", nullable = true, insertable = true, updatable = true)
    public Timestamp getDeathDate() {
        return deathDate;
    }

    public void setDeathDate(Timestamp ddate) {
        this.deathDate = ddate;
    }

    @Basic
    @Column(name = "IMG", nullable = true, insertable = true, updatable = true, length = 100)
    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    @Basic
    @Column(name = "BIO", nullable = true, insertable = true, updatable = true, length = 4000)
    public String getBio() {
        return bio;
    }

    public void setBio(String bio) {
        this.bio = bio;
    }

    @OneToMany
    @JoinColumn(name = "AUTHOR_ID")
    public List<Book> getBooks() {
        return books;
    }

    public void setBooks(List<Book> books) {
        this.books = books;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Author author = (Author) o;

        if (authorId != author.authorId) return false;
        if (birthDate != null ? !birthDate.equals(author.birthDate) : author.birthDate != null) return false;
        if (bio != null ? !bio.equals(author.bio) : author.bio != null) return false;
        if (deathDate != null ? !deathDate.equals(author.deathDate) : author.deathDate != null) return false;
        if (firstName != null ? !firstName.equals(author.firstName) : author.firstName != null) return false;
        if (img != null ? !img.equals(author.img) : author.img != null) return false;
        if (lastName != null ? !lastName.equals(author.lastName) : author.lastName != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        long result = authorId;
        result = 31 * result + (firstName != null ? firstName.hashCode() : 0);
        result = 31 * result + (lastName != null ? lastName.hashCode() : 0);
        result = 31 * result + (birthDate != null ? birthDate.hashCode() : 0);
        result = 31 * result + (deathDate != null ? deathDate.hashCode() : 0);
        result = 31 * result + (img != null ? img.hashCode() : 0);
        result = 31 * result + (bio != null ? bio.hashCode() : 0);
        return ((Long) result).intValue();
    }
}
